/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package message;
import java.util.*;
import java.io.*;
import net.*;
import structure.*;

/**
 *
 * @author antonio
 */
public class MessagePosition extends Message{

    private AccessPoint infoPosition;

    public MessagePosition(AccessPoint ap)
    {
        this.infoPosition = ap;
    }


    
    public AccessPoint getInfoPosition()
    {
        return this.infoPosition;
    }

    public void estimatePosition()
    {
        Vector region = this.selectRegionOfSpace();
    }

    private Vector selectRegionOfSpace()
    {
        int numTokens;
        int maxMac = 0;  //numero massimo di mac corrispondenti
        
        String tmpRegion;  //stanza di una liena del file
        Vector region = new Vector(); //stanze selezionate , con i mac corrispondenti

        try{
            File mapRegionsOfSpace = new File("E:\\Documents and Settings\\antonio\\Documenti\\mappa.txt");
            Vector selectedRegion = new Vector();

            BufferedReader mapFile = new BufferedReader(new FileReader(mapRegionsOfSpace));

            while(true)
            {
                Vector mac = new Vector();  //mac di una linea del file
                String line = mapFile.readLine();
                if(line.equals(null))
                {
                    break;
                }
                else
                {
                    StringTokenizer tokenizer = new StringTokenizer(line, ",=");
                    numTokens = tokenizer.countTokens();
                    for(int i=0; i<(numTokens-1); i++)
                        mac.add(i, tokenizer.nextToken());
                    tmpRegion = tokenizer.nextToken(); //l'ultimo token è la stanza

                    int countMacInOneLine = 0;
                    for(int i=0; i<mac.size(); i++)
                    {
                        String tmpMac = (String)mac.elementAt(i);
                        for(int j=0; j<this.infoPosition.getMac().size(); j++)
                        {
                            String macSniffato = ((String)this.infoPosition.getMac().elementAt(j));
                            if(tmpMac.equals(macSniffato))
                            {
                                countMacInOneLine++;
                                break;
                            }
                        }
                    }

                    if(countMacInOneLine >= maxMac)
                    {
                        maxMac = countMacInOneLine;
                        region.add(tmpRegion);
                    }

                }
            }

        }catch(Exception e)
        {
            e.printStackTrace();
        }

        return region;
        
    }
    public void printStructure()
    {
        System.out.println("PRINT STRUCTURE...");
        Enumeration e = Structurer.realWorld.keys();
        while(e.hasMoreElements())
        {
            String key = (String)e.nextElement();
            System.out.println(key);
            Hashtable pointOfStructure = (Hashtable)Structurer.realWorld.get(key);
            Enumeration frequency = pointOfStructure.elements();
            while(frequency.hasMoreElements())
                System.out.println( ( (Integer)frequency.nextElement() ).intValue() );
            System.out.println("-------END ELEMENT-------");
        }
    }
}
